package com.yangyang.ralearn.server;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.yangyang.ralearn.common.Constants;
import com.yangyang.ralearn.common.JEnvironment;
import com.yangyang.ralearn.common.entity.base.ActionLimit;
import com.yangyang.ralearn.common.entity.base.BaseActionModelDef;
import com.yangyang.ralearn.common.entity.base.ParamedPredicate;

public class JServerEnvironment extends JEnvironment {

	public transient Map<String, BaseActionModelDef> mapNameActionModelDef = new HashMap<String, BaseActionModelDef>();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public static JServerEnvironment newInstance(int id) {
		return new JServerEnvironment();
	}

	@Override
	public void clearData() {
		// TODO Auto-generated method stub
		super.clearData();
		this.mapNameActionModelDef.clear();
	}

	public void addActionModelDef(String name, List<String> param,
			List<String> pre, List<String> eff) {
		// TODO Auto-generated method stub
		LinkedHashMap<String, String> mapParameter = new LinkedHashMap<String, String>();
		for (String line : param) {
			String[] array = line.split("-");
			String[] subarray = array[0].split(" ");
			for (String s : subarray)
				mapParameter.put(s, array[1]);
		}
		BaseActionModelDef def = new BaseActionModelDef(this, name,
				mapParameter, pre, eff);
		this.mapNameActionModelDef.put(name, def);
		ActionLimit limit = new ActionLimit(name);
		limit.setMapVar2Type(def.getParamDef());
		limit.setPreConditionCount(def.getPreConditions().size());
		for (ParamedPredicate predicate : def.getEffects()) {
			if (predicate.getName().startsWith(Constants.NEG_PREFIX))
				limit.setEffectDeleteCount(limit.getEffectDeleteCount() + 1);
			else
				limit.setEffectAddCount(limit.getEffectAddCount() + 1);
		}
		this.mapName2ActionLimit.put(name, limit);
	}

	@Override
	public String toString() {
		return "JServerEnvironment [mapNameActionModelDef="
				+ mapNameActionModelDef + ", mapNameType=" + mapNameType
				+ ", mapNamePredicateDef=" + mapNamePredicateDef
				+ ", mapType2Object=" + mapType2Object + ", mapName2Object="
				+ mapName2Object + ", mapName2ActionLimit="
				+ mapName2ActionLimit + "]";
	}

}
